package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.form.CategoryTable;
import com.tjnu.losca.mapper.CategoryMapper;
import com.tjnu.losca.pojo.Category;
import com.tjnu.losca.pojo.Goods;
import com.tjnu.losca.service.ICategoryService;
import com.tjnu.losca.service.IGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author losca
 * @since 2022-02-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    IGoodsService goodsService;
    @Override
    public IPage<Category> getAll(Integer pageNum, Integer pageSize, String search) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            wrapper.eq("name", search);
        }
        return this.page(new Page<>(pageNum,pageSize), wrapper);

    }

    //CacheEvict注解 清除category::category缓存到Redis里
    @CacheEvict(value = "category",key = "'category'")
    @Override
    public boolean del(String cId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("categoryId", cId);
        Category category = this.getOne(wrapper);
        category.setStatus(0);
        return this.updateById(category);
    }

    @CacheEvict(value = "category",key = "'category'")
    @Override
    public boolean rec(String cId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("categoryId", cId);
        Category category = this.getOne(wrapper);
        category.setStatus(1);
        return this.updateById(category);
    }

    @CacheEvict(value = "category",key = "'category'")
    @Override
    public boolean mulDel(String[] cIds) {
        try {
            for (String cId : cIds) {
                del(cId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @CacheEvict(value = "category",key = "'category'")
    @Override
    public boolean add(Integer categoryId,String name) {
        Category category = new Category();
        category.setName(name);
        category.setCategoryId(categoryId);
        return this.save(category);
    }

    @Override
    public HashMap getData() {
        HashMap<Integer, String> map = new HashMap<>();
        List<Category> list = this.list();
        for (Category category : list) {
            map.put(category.getCategoryId(), category.getName());
        }
        System.out.println(map.toString());
        return map;
    }

    @Override
    public List<CategoryTable> categoryTable() {
        ArrayList<CategoryTable> categoryTables = new ArrayList<>();
        //查询所有商品分类数据
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        List<Category> list = this.list(wrapper);
        //对商品分类数据进行遍历，根据分类id在商品表查询商品数量
        for (Category category : list) {
            Integer categoryId = category.getCategoryId();
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("category_id",categoryId);
            int count = goodsService.count(goodsQueryWrapper);
            CategoryTable categoryTable = new CategoryTable();
            categoryTable.setName(category.getName());
            categoryTable.setData(count);
            categoryTables.add(categoryTable);
        }
        return categoryTables;
    }
}
